﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;
using MySql.Data.MySqlClient;

namespace ESpace.DalTools.Common
{
	public interface ITestConnection
	{
		bool TestConnection(string connstr);
	}

	public class SqlServerConn : ITestConnection
	{


		public bool TestConnection(string connstr)
		{

			try
			{
				System.Data.SqlClient.SqlConnection connTest = new System.Data.SqlClient.SqlConnection(connstr);
				connTest.Open();
				connTest.Close();
				return true;
			}
			catch
			{
				return false;
			}

		}
	}

	public class MySqlConn : ITestConnection
	{
		#region ITestConnection 成员

		public bool TestConnection(string connstr)
		{
			try
			{
				MySqlConnection mysql = new MySqlConnection(connstr);
				mysql.Open();
				mysql.Close();
				return true;
			}
			catch
			{
				return false;
			}
		}

		#endregion
	}

	public class SQLiteConn : ITestConnection
	{
		#region ITestConnection 成员

		public bool TestConnection(string connstr)
		{
			try
			{

				using (SQLiteConnection sqliteConn = new SQLiteConnection(connstr))
				{
					sqliteConn.Open();

					//SHOW COLUMNS FROM
					//SQLiteCommand cmd = new SQLiteCommand("SHOW COLUMNS FROM RuleForm", sqliteConn);
					//SQLiteDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

					sqliteConn.Close();
				}
				return true;

			}
			catch
			{
				return false;
			}

		}

		#endregion
	}

	public class AccessConn : ITestConnection
	{
		#region ITestConnection 成员

		public bool TestConnection(string connstr)
		{
			throw new NotImplementedException();
		}

		#endregion
	}


	public class TestConnectionFactory
	{
		public ITestConnection Creator(string type)
		{
			switch (type)
			{
				case "SqlServer":
					return new SqlServerConn();

				case "MySql":
					return new MySqlConn();

				case "Sqlite":
					return new SQLiteConn();

				case "Access":
					return new AccessConn();

				default:
					return new SqlServerConn();
			}
		}
	}




}
